package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.WindowManager;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;
import com.echoesnet.echoesshortvideo.Utils.VideoUtilListener;
import com.echoesnet.echoesshortvideo.Utils.VideoUtils;
import com.echoesnet.echoesshortvideo.views.cameraView.CameraView;
import com.echoesnet.echoesshortvideo.views.cameraView.lisenter.CameraLisenter;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import static com.echoesnet.echoesshortvideo.views.cameraView.CameraView.BUTTON_STATE_ONLY_RECORDER;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/24 0024
 * @description
 */
public class CameraAct extends BaseActivity
{
    private final static String TAG = CameraAct.class.getSimpleName();
//    private final static String CAMERA_VIDEO_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator +
//            "Camera";
    private final static String CAMERA_VIDEO_PATH = CommonUtils.getRootDirPath(AppController.getInstance()) +
            "Camera";
    @BindView(R.id.cameraView)
    CameraView cameraView;

    private Activity mAct;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   // 不锁屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_act);
        ButterKnife.bind(this);
        mAct = this;

        cameraView.setFeatures(BUTTON_STATE_ONLY_RECORDER);
        cameraView.setSaveVideoPath(CAMERA_VIDEO_PATH);
        cameraView.setCameraLisenter(new CameraLisenter()
        {
            @Override
            public void captureSuccess(String showType, Bitmap bitmap, String url)
            {

            }

            @Override
            public void recordSuccess(String showType, String url, Bitmap firstFrame, long duration,int degree)
            {
//                String fileKeyName = "a_" + SharePreUtils.getUId() + UUID.randomUUID().toString().substring(0, 8) + ".jpg";
//                String outPutImagePath = CommonUtils.getRootDirPath(mAct) + "camera" + File.separator + CommonUtils.toMD5(fileKeyName);
//                Observable.create(new ObservableOnSubscribe<String>()
//                {
//                    @Override
//                    public void subscribe(ObservableEmitter<String> emitter) throws Exception
//                    {
//                      File file = CommonUtils.saveBitmap(firstFrame,100,outPutImagePath,0);
//                      Logger.t(TAG).d("缩略图>>>" + file.getAbsolutePath() + " | url>>>" + url);
//                      emitter.onNext(file.getAbsolutePath());
//                    }
//                }).subscribeOn(Schedulers.computation())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Consumer<String>()
//                        {
//                            @Override
//                            public void accept(String s) throws Exception
//                            {
//
//                            }
//                        });
//                String output = CAMERA_VIDEO_PATH + File.separator + System.currentTimeMillis() + ".mp4";
//                VideoUtils.reversalVideo(url, output, new VideoUtilListener()
//                {
//                    @Override
//                    public void start()
//                    {
//
//                    }
//
//                    @Override
//                    public void complete(String outPath, String err)
//                    {
//
//                    }
//                });
                Logger.t(TAG).d("拍摄完毕" + url);
                Intent intent = new Intent(mAct,PublishVideoAct.class);
                intent.putExtra("path",url);
                intent.putExtra("showType",showType);
                intent.putExtra("degree",degree);
                intent.putExtra("isCamera",true);
                startActivity(intent);
                finish();
            }

            @Override
            public void quit()
            {
                finish();
            }
        });
    }

    @Override
    protected void onResume()
    {
        if (cameraView != null)
            cameraView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        if (cameraView != null)
            cameraView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        cameraView.onDestroy();
        super.onDestroy();
    }
}
